
import { _decorator, Component, Node, v3, v2 } from 'cc';
import { ParticleVecItem } from './ParticleVecItem';
const { ccclass, property, executeInEditMode } = _decorator;

@ccclass('CaidaiParticle')
@executeInEditMode
export class CaidaiParticle extends ParticleVecItem {
    // onLoad() {
    //     this.init();
    // }

    // onEnable() {
    //     this.play();
    // }

    protected particleCount: number = 100;
    protected createAnim() {
        let Pos = [0, 0, 0, 0];
        let Time = [3, 0, 0, 0]; //[0] 消失的时间
        let Ease = [0, 0, 0, 0];
        let lineVec = [0, 0, 0, 0];
        let dampVec = [0, 0, 0, 0];
        let angVec = [0, 0, 0, 0];
        let angDampingVec = [0, 0, 0, 0];
        //线性速度
        let speed = v3(4, 50, 4);
        let damping = v3(1, -20, 1);
        //角速度
        let angSpeed = v3(1000, 600, 1500).multiplyScalar(0.01745);
        let angDamping = v3(0.2, 0.2, 0.2);

        let index = 0;
        let tmpPos = v2();
        let len = 0;
        for (let i = 0; i < this.particleCount; ++i) {
            len = Math.random() * speed.x;
            tmpPos.set(len, 0);
            let radian = Math.random() * 6.28 - 3.14;
            tmpPos.rotate(radian);
            let x = tmpPos.x;
            let z = tmpPos.y;
            let y = speed.y * (Math.random() * 0.8 + 0.2);

            //线性速度
            lineVec[0] = x;
            lineVec[1] = y;
            lineVec[2] = z;

            //衰减速度
            dampVec[0] = damping.x * (x > 0 ? -1 : 1);
            dampVec[1] = damping.y;    //重力加速度
            dampVec[2] = damping.z * (z > 0 ? -1 : 1);

            //角速度
            angVec[0] = angSpeed.x * (Math.random() - 0.5);
            angVec[1] = angSpeed.y * (Math.random() - 0.5);
            angVec[2] = angSpeed.z * (Math.random() - 0.5);

            //角速度衰减速度
            angDampingVec[0] = -angVec[0] * angDamping.x;
            angDampingVec[1] = -angVec[1] * angDamping.y;
            angDampingVec[2] = -angVec[2] * angDamping.z;

            this.cacheParticleAnim(index++, Pos, lineVec, dampVec, angVec, angDampingVec, Time, Ease);

        }
    }

}